using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Codex
{
    public class Codex
    {
        public List<Entry> entryList = new List<Entry>();

        private string blogUserName;
        private string blogPassword;
        private string blogID;
        private string blogEmail;

        public Codex() { }

        //Blog methods
        public void setBlogUserName(string _blogUserName) { blogUserName = _blogUserName; }
        public void setBlogPassword(string _blogPassword) { blogPassword = _blogPassword; }
        public void setBlogID(string _blogID) { blogID = _blogID; }
        public void setBlogEmail(string _blogEmail) { blogEmail = _blogEmail; }

        public string getBlogUserName() { return blogUserName; }
        public string getBlogPassword() { return blogPassword; }
        public string getBlogID() { return blogID; }
        public string getBlogEmail() { return blogEmail; }


        public List<String> getAllLabels()
        {
            List<String> labelList = new List<string>();

            foreach (Entry tempEntry in entryList)
            {
                foreach (string label in tempEntry.getLabelList())
                {
                    if (!labelList.Contains(label))
                        labelList.Add(label);
                }
            }

            return labelList;
        }

        public List<string> getAllFieldNames()
        {
            List<string> fieldList = new List<string>();

            foreach (Entry tempEntry in entryList)
            {
                foreach (FieldTag tempField in tempEntry.getFieldList())
                {
                    if (!fieldList.Contains(tempField.getFieldName()))
                        fieldList.Add(tempField.getFieldName());
                }
            }

            return fieldList;
        }

        public void deleteAllOccurrencesOfLabel(string tag)
        {
            foreach (Entry tempEntry in entryList)
            {
                if (tempEntry.hasLabel(tag)) tempEntry.removeLabel(tag);
            }
        }

        public void renameAllOccurrencesOfTag(string label, string newLabel)
        {
            foreach (Entry tempEntry in entryList)
            {
                if (tempEntry.hasLabel(label))
                {
                    tempEntry.removeLabel(label);
                    tempEntry.addLabel(newLabel);
                }
            }
        }

        public void addEntry(Entry newEntry) { entryList.Add(newEntry); }

        public void sortByDate() { entryList.Sort(); }

        public int size() { return entryList.Count; }

        public List<Entry> getEntryList() { return entryList; }

        public Entry getEntry(DateTime entryDate)
        {
            foreach (Entry entry in entryList)
            {
                //Debug:Console.WriteLine("Comparing " + entryDate + " to " + entry.getEntryDate() 
                    //Debug:+ " - " + entry.getEntryDate().ToString().Equals(entryDate.ToString()));
                
                if (entry.getEntryDate().ToString().Equals(entryDate.ToString())) 
                    return entry;
            }

            throw new FieldAccessException();
        }

        public Entry getEntry(int index) { return entryList[index]; }

        public void removeEntry(Entry rEntry)
        {
            entryList.Remove(rEntry);
        }

        public int getNumEntries() { return entryList.Count; }

        public override String ToString()
        {
            String outputString = "";

            foreach (Entry tempEntry in entryList)
            {
                outputString += tempEntry.ToString();
            }

            return outputString;
        }
    }
}
